package com.danielepelagatti.controls
{
	import flash.display.DisplayObject;
	
	import mx.controls.Button;
	import mx.controls.LinkButton;
	import mx.core.IFlexDisplayObject;
	import mx.core.mx_internal;
	import mx.styles.ISimpleStyleClient;
	import mx.controls.buttonBarClasses.ButtonBarButton
	use namespace mx_internal;
	/**
	 * 
	 * @author redde
	 * 
	 */	
	public class SizeableIconButtonBarButton extends ButtonBarButton
	{	
		private var _iconHeight:Number;
		private var _iconWidth:Number;
		private var icons:Array /* of Sprite */ = [];
		 
		/**
		 * 
		 * @param value
		 * 
		 */
		[Inspectable(type="String",enumeration="128x128,96x96,72x72,64x64,48x48,36x36,32x32,24x24,22x22,16x16",defaultVaule="128x128")]
		public function set iconSize(value:String):void
		{
			_iconHeight = _iconWidth = Number(value.substring(0,value.indexOf("x")));
		}
		/**
		* 
		*/		
		override mx_internal function viewIconForPhase(tempIconName:String):void
		{
			// Has this icon already been created?
	        var newIcon:IFlexDisplayObject =
	            IFlexDisplayObject(getChildByName(tempIconName));
	
	        // If not, create it.
	        if (newIcon == null)
	        {
	            //get specific icon style
	            var newIconClass:Class = Class(getStyle(tempIconName));
	            //default to the more general icon style
	            if (newIconClass == null)
	                newIconClass = Class(getStyle(iconName));
	
	            if (newIconClass != null)
	            {
	                newIcon = IFlexDisplayObject(new newIconClass());
	                if(_iconWidth)
						newIcon.width = _iconWidth;
					if(_iconHeight)
						newIcon.height = _iconHeight;
	                // Set its name so that we can find it in the future
	                // using getChildByName().
	                newIcon.name = tempIconName;
	
	                if (newIcon is ISimpleStyleClient)
	                    ISimpleStyleClient(newIcon).styleName = this;
	
	                addChild(DisplayObject(newIcon));
	
	                // Keep track of all skin children that have been created.
	                icons.push(newIcon);
	            }
	        }
	
	        // Hide the old icon.
	        if (currentIcon != null)
	            currentIcon.visible = false;
	
	        // Keep track of which skin is current.
	        currentIcon = newIcon;
	
	        // Show the new skin.
	        if (currentIcon != null)
	            currentIcon.visible = true;			
		}
		/**
		* 
		*/		
		override mx_internal function getCurrentIcon():IFlexDisplayObject
	    {
	        // Determine which icon will get displayed, based on whether this
	        // Button is enabled or disabled, whether it is
	        // selected or unselected, and how it is currently interacting
	        // with the mouse (i.e., the up/over/down state).
	
	        var tempIconName:String = getCurrentIconName(); 
	
	        if (!tempIconName)
	            return null;
	
	        // Has this icon already been created?
	        var newIcon:IFlexDisplayObject =
	            IFlexDisplayObject(getChildByName(tempIconName));
	
	        // If not, create it.
	        if (newIcon == null)
	        {
	            //get specific icon style
	            var newIconClass:Class = Class(getStyle(tempIconName));
	            //default to the more general icon style
	            if (newIconClass == null)
	                newIconClass = Class(getStyle(iconName));
	
	            if (newIconClass != null)
	            {
	                newIcon = IFlexDisplayObject(new newIconClass());
	                if(_iconWidth)
						newIcon.width = _iconWidth;
					if(_iconHeight)
						newIcon.height = _iconHeight;	
	                // Set its name so that we can find it in the future
	                // using getChildByName().
	                newIcon.name = tempIconName;
	
	                if (newIcon is ISimpleStyleClient)
	                    ISimpleStyleClient(newIcon).styleName = this;
	
	                addChild(DisplayObject(newIcon));
	
	                // Keep track of all skin children that have been created.
	                icons.push(newIcon);
	            }
	        }
	
	        return newIcon;
	    }
	}
}